﻿using Microsoft.AspNetCore.Mvc;
using SYSoft.Comm;
using SYSoft.Comm.Utils;
using SYSoft.Model;
using SYSoft.Model.Models.DeviceManage.Maintain;
using SYSoft.Services.MaintainManage;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace SYSoft.Controllers.MaintainManage
{
    public class WH_BaselibController : BaseController
    {

        IWH_BaselibService wh;
        public WH_BaselibController(IWH_BaselibService wh)
        {
            this.wh = wh;
        }

        /// <summary>
        /// 获取基础库该设备名称
        /// </summary>
        /// <param name="devicename">2012-09-21</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<string>>> GetAllDeviceName()
        {

            var model = await wh.Query(x => x.CreatTime < Convert.ToDateTime("1999-11-11 23:00:00"));
            if (model != null && model.Count > 0)
            {
                List<string> str = new List<string>();
                for (int i = 0; i < model.Count; i++)
                {
                    if (!str.Contains(model[i].DeviceFlag))
                        str.Add(model[i].DeviceFlag);
                }
                return MessageModel.CreateSuccessResult(str);
            }
            return MessageModel.CreateFailedResult<List<string>>("");
        }


        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="devicename">2012-09-21</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel> PostDeviceName(string devicename)
        {
            WH_Baselib model = new WH_Baselib
            {
                Flag = -1,
                PlanTime = Convert.ToDateTime("1998-11-11 00:00:00"),
                CreatTime = Convert.ToDateTime("1998-11-11 00:00:00"),
                DeviceFlag = devicename,
            };
            var res = await wh.Add(model);
            if (res > 0)
            {
                return MessageModel.CreateSuccessResult();
            }
            return new MessageModel("添加设备失败！！");
        }



        /// <summary>
        /// 获取基础库该设备所有任务
        /// </summary>
        /// <param name="devicename">2012-09-21</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<PageModel<WH_Baselib>>> GetTasksByDeviceName(string devicename, int page = 1, int size = 10)
        {

            var model = await wh.QueryPage((item => item.DeviceName == devicename), page, size);
            if (model.data != null && model.data.Count > 0)
            {
                return MessageModel.CreateSuccessResult(model);
            }
            return MessageModel.CreateFailedResult<PageModel<WH_Baselib>>("");
        }


      


        /// <summary>
        /// 导出获取基础库所有任务 Excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportAllBaseWhTasks()
        {
     
            DataTable dataTable = null;

            var model = await wh.Query(x => x.CreatTime > Convert.ToDateTime("2011-11-11 23:00:00"));
            if (model != null && model.Count > 0)
            {
                dataTable = UtilConvert.ListToDt(model);
                if (dataTable == null || dataTable.Rows.Count < 1)
                    return NotFound("未查询到数据！！");
                string name = "数据详情";
                byte[] bytes = ExcelHelper.DataTableToExcel(dataTable);
                return File(bytes, "application/octet-stream", $"{name}_{DateTime.Now:yyyyMMddHHmmssfff}.xls");
            }
            else
            {
                return NotFound("未查询到数据！！");
            }                  
        }



        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<int>> Post([FromBody] WH_Baselib model)
        {
            var res = await wh.Add(model);
            if (res > 0)
                return MessageModel.CreateSuccessResult(res);
            else
                return MessageModel.CreateFailedResult<int>("Id必须大于0");
        }



        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Update([FromBody] WH_Baselib model)
        {

            //sysUserInfo.UpdateBy = Operator.UserId; //操作者
            var res = await wh.Update(model);
            if (res)
                return MessageModel.CreateSuccessResult<string>("sucess");
            return MessageModel.CreateFailedResult<string>("修改任务信息失败！！");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id)
        {
            if (id > 0)
            {
                var userDetail = await wh.QueryById(id);
                bool res = await wh.Delete(userDetail);
                if (!res)
                    return MessageModel.CreateFailedResult<string>("删除任务失败！！");
                return MessageModel.CreateSuccessResult("删除任务成功");
            }
            return MessageModel.CreateFailedResult<string>("Id必须大于0");
        }
    }
}
